package com.example.shopsystem.aop;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * @author : xhp
 * @version : [v1.0]
 * @description : [token的封装类，封装token的一系列方法]
 * @createTime : [2022/1/8 15:45]
 * @updateUser : [17151]
 * @updateTime : [2022/1/8 15:45]
 * @updateRemark : [说明本次修改内容]
 */
public class TokenMethod extends HttpServlet {
    protected void getToken(HttpServletRequest req, HttpServletResponse resp, String org) throws ServletException, IOException {
        String base = TokenMethod.token(org);//获取token
        //把token返回给用户
        resp.setHeader("Token",base);
    }
    //判断是否为之前的token
    public static String isToken( String[] str){
        String oldIp = str[0];//得到ip
        String oldPwd = str[1];//密码
        long oldExTime = Long.parseLong(str[2]);//之前的过期时间
        String type=str[3];//得到用户类型
        String oldMd5 = str[4];//之前的MD5
        long newTime = System.currentTimeMillis() + 30000;
        String org = oldIp + ";" + oldPwd + ";" + oldExTime+";"+type;
        String md5 = getMd5(org);//得到解析后的MD5
        String newToken = "";
        if (md5.equals(oldMd5)&&oldExTime>System.currentTimeMillis()) {
            String newOrg = oldIp + ";" + oldPwd + ";" + newTime+";"+type;
            newToken = token(newOrg);
        }
        return newToken;
    }

    //解析token
    public static String[] decomToken(String org) {
        //先把token base64转回字符串
        String str = baseConvertStr(org);
        String[] str1 = str.split(";");
        return str1;
    }

    //把base64重新转回字符串
    public static String baseConvertStr(String str) {
        if (null != str) {
            Base64.Decoder decoder = Base64.getDecoder();
            try {
                return new String(decoder.decode(str.getBytes()), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                return null;
            }
        }
        return null;
    }

    //生成token
    public static String token(String org) {
        //把用户信息加上过期时间转成MD5码
        String md5 = getMd5(org);
        //再把用户信息和刚刚的MD5码加起来
        org = org + ";" + md5;
        //最后把org转换成base64
        String base = new String(Base64.getEncoder().encode(org.getBytes()));
        return base;
    }

    //封装成MD5
    public static String getMd5(String org) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
            md.reset();
            md.update(org.getBytes(StandardCharsets.UTF_8));
            byte[] byteArray = md.digest();
            StringBuilder md5StrBuff = new StringBuilder();
            for (int i = 0; i < byteArray.length; i++) {
                if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
                    md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
                else
                    md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
            }

            return md5StrBuff.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}
